package com.ruoyi.report.forms.util;

import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.report.forms.domain.CngScadaRealtimeMonitoringHour;
import com.ruoyi.report.forms.domain.DispatchAccessDataDay;
import com.ruoyi.report.forms.domain.UpdateReport;
import com.ruoyi.report.forms.service.ICngScadaRealtimeMonitoringHourService;
import com.ruoyi.report.forms.service.IDispatchAccessDataDayService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataUpdate {

    public static void Update(UpdateReport updateReport) {
        String type = updateReport.getBind().get("type");
        switch (type) {
            case "station_day":// 场站设备小时数据
                DataUpdate.Update_Station_Day(updateReport);
                break;
            case "station_month":
                DataUpdate.Update_Station_Month(updateReport);
                break;
            case "into_out":// 接入点数据
                DataUpdate.Update_Into_Out(updateReport);
                break;
        }
    }

    // 更新场站日数据
    private static void Update_Station_Day(UpdateReport updateReport) {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = sdFormat.parse(updateReport.getDate() + " 08:00:00");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 根据选中的单元格判断要修改的值是哪一个时间的
        int startIndex = Integer.parseInt(updateReport.getBind().get("r_start"));
        int currentIndex = Integer.parseInt(updateReport.getSelectR());
        calendar.add(Calendar.HOUR, currentIndex - startIndex);
        Date hourDate = calendar.getTime();

        CngScadaRealtimeMonitoringHour cngScadaRealtimeMonitoringHour = new CngScadaRealtimeMonitoringHour();
        cngScadaRealtimeMonitoringHour.setAttributeId(updateReport.getBind().get("attributeId"));
        cngScadaRealtimeMonitoringHour.setDate(sdFormat.format(hourDate));
        cngScadaRealtimeMonitoringHour.setMonitoringValue(updateReport.getValue());

        ICngScadaRealtimeMonitoringHourService cngScadaRealtimeMonitoringHourService = SpringUtils.getBean(ICngScadaRealtimeMonitoringHourService.class);
        cngScadaRealtimeMonitoringHourService.updateCngScadaRealtimeMonitoringCustomHour(cngScadaRealtimeMonitoringHour);
    }

    private static void Update_Station_Month(UpdateReport updateReport) {
        int startIndex = Integer.parseInt(updateReport.getBind().get("r_start"));
        int currentIndex = Integer.parseInt(updateReport.getSelectR());

        String hour = updateReport.getBind().get("hour");
        String time;
        if (hour == null || hour.isEmpty()) {
            time = "07:00:00";
        } else {
            time = String.format("%02d", Integer.parseInt(hour)) + ":00:00";
        }
        String date = updateReport.getMonth() + "-" + String.format("%02d", currentIndex - startIndex + 1) + " " + time;

        CngScadaRealtimeMonitoringHour cngScadaRealtimeMonitoringHour = new CngScadaRealtimeMonitoringHour();
        cngScadaRealtimeMonitoringHour.setAttributeId(updateReport.getBind().get("attributeId"));
        cngScadaRealtimeMonitoringHour.setDate(date);
        cngScadaRealtimeMonitoringHour.setMonitoringValue(updateReport.getValue());

        ICngScadaRealtimeMonitoringHourService cngScadaRealtimeMonitoringHourService = SpringUtils.getBean(ICngScadaRealtimeMonitoringHourService.class);
        cngScadaRealtimeMonitoringHourService.updateCngScadaRealtimeMonitoringCustomHour(cngScadaRealtimeMonitoringHour);
    }

    // 更新接入点数据
    private static void Update_Into_Out(UpdateReport updateReport) {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = sdFormat.parse(updateReport.getDate());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        int startIndex = Integer.parseInt(updateReport.getBind().get("r_start"));
        int currentIndex = Integer.parseInt(updateReport.getSelectR());

        DispatchAccessDataDay dispatchAccessDataDay = new DispatchAccessDataDay();
        dispatchAccessDataDay.setAccessId(updateReport.getBind().get("intoId"));
        dispatchAccessDataDay.setDataTime(date);

        // 根据选中的单元格判断要修改的字段是哪一个
        int number = currentIndex - startIndex;
        if (number == 2) {
            dispatchAccessDataDay.setReading(updateReport.getValue());
        } else if (number == 4) {
            dispatchAccessDataDay.setPressure(updateReport.getValue());
        } else if (number == 6) {
            dispatchAccessDataDay.setFlow(updateReport.getValue());
        }

        IDispatchAccessDataDayService dispatchAccessDataDayService = SpringUtils.getBean(IDispatchAccessDataDayService.class);
        dispatchAccessDataDayService.updateDispatchAccessDataDay(dispatchAccessDataDay);
    }
}
